package main

import (
	"context"
	"fmt"
	"net"
)

type Server struct {
	Port string
	ctx  context.Context
}

func NewServer(ctx context.Context, port string) *Server {
	return &Server{
		Port: port,
		ctx:  ctx,
	}
}

// 启动服务
func (this *Server) Start() error {
	listen, err := net.Listen("tcp", "0.0.0.0:"+this.Port)
	if err != nil {
		panic(err)
	}

	fmt.Printf("服务启动成功 地址：%v", "0.0.0.0:"+this.Port)

	for {
		conn, err := listen.Accept()
		if err != nil {
			fmt.Println("accept failed, err:", err)
			continue
		}

		client := NewClient(this.ctx, conn.RemoteAddr().String(), conn)

		ManageClient.AddClient(client)
	}
}
